package cn.tedu;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 爬虫数据持久化  组件
 */
public class DadaPip extends JDBC implements Pipeline {
    public void process(ResultItems resultItems, Task task) {
        Object title_tex = resultItems.get("title_tex");
        Object time_tex = resultItems.get("time_tex");
        Object time_ly = resultItems.get("time_ly");
        Object elementsByClass = resultItems.get("elementsByClass");
        Object bj = resultItems.get("bj");
        Object currChnlCls = resultItems.get("currChnlCls");
        //对数据进行处理
        String[] split = time_tex.toString().split("：");
        String time = split[split.length-1];

        String[] split1 = time_ly.toString().split("：");
        String ly = split1[split1.length-1];

        String[] split2 = bj.toString().split("：");
        String auhtor = split2[split2.length-1];

        String[] s = currChnlCls.toString().split(" ");
        String type = s[s.length-1];

//        System.out.println("爬虫的数据持久化："+title_tex);
//        System.out.println("爬虫的数据持久化：time_tex:"+time);
//        System.out.println("爬虫的数据持久化：time_ly"+ly);
//        System.out.println("爬虫的数据持久化：bj"+auhtor);
//        System.out.println("爬虫的数据持久化：currChnlCls"+type);
//        System.out.println("爬虫的数据持久化：elementsByClass"+elementsByClass);

        //保存数据到   数据库
        //添加党建信息类型到数据库中

        add("INSERT INTO newstype(NAME) VALUES(?)",type);

        int i = add("INSERT INTO news(title,auhtor,content,createtme,fromnews,type) VALUES(?,?,?,?,?,?)",
                title_tex,auhtor,elementsByClass.toString(),time,ly,type);
        //判断是否添加成功
        if(i>0){
            System.out.println("爬虫的数据持久化："+title_tex);
            System.out.println("爬虫的数据持久化：time_tex:"+time);
            System.out.println("爬虫的数据持久化：time_ly"+ly);
            System.out.println("爬虫的数据持久化：bj"+auhtor);
            System.out.println("爬虫的数据持久化：currChnlCls"+type);
            System.out.println("爬虫的数据持久化：elementsByClass"+elementsByClass);
        }
    }
}
